#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, re, os
import time
from datetime import date
from sheepomatic_io import Csv_file,Sheep_brief_file,Shepherd_brief_file
from sheepomatic import Sheep,Shepherd
import sheepomatic

class Pasture_info:
   @classmethod
   def print_info(cls,sheep_list,shepherd_list):
      """Prints information about the sheep-shepherd dataset
  
      Parameters:
      sheep_list -- sheep object list
      shepherd_list -- shepherd obbject list
      """
      # FACULTY INFORMATION
      # collect
      all_faculties = set([])
      for sheep in sheep_list:
         all_faculties.add(sheep.faculty)
      for shepherd in shepherd_list:
         all_faculties.add(shepherd.faculty)
      fac_sheep = {}
      fac_shepherd_count = {}
      fac_shepherd_capacity = {}
      for faculty in all_faculties:
         fac_sheep[faculty] = 0
         fac_shepherd_count[faculty] = 0
         fac_shepherd_capacity[faculty] = 0
      for sheep in sheep_list:
         fac_sheep[sheep.faculty] += 1
      for shepherd in shepherd_list:
         fac_shepherd_count[shepherd.faculty] += 1
         fac_shepherd_capacity[shepherd.faculty] += shepherd.capacity
      # print
      print "FACULTY INFORMATION (sheep count - shepherd count - shepherd capacity):"
      for fac in all_faculties:
         print fac+":\t" + str(fac_sheep[fac]) +\
                    "\t" + str(fac_shepherd_count[fac]) + "\t" + str(fac_shepherd_capacity[fac])
      # DATES
      # collect
      sheep_date_freq_hash = {}
      shepherd_date_freq_hash = {}
      shepherd_date_capacity_hash = {}
      for sheep in sheep_list:
         if sheep.arrival_date not in sheep_date_freq_hash:
            sheep_date_freq_hash[sheep.arrival_date] = 0
         sheep_date_freq_hash[sheep.arrival_date] += 1
      for shepherd in shepherd_list:
         if shepherd.ava_date not in shepherd_date_freq_hash:
            shepherd_date_freq_hash[shepherd.ava_date] = 0
         shepherd_date_freq_hash[shepherd.ava_date] += 1
         if shepherd.ava_date not in shepherd_date_capacity_hash:
            shepherd_date_capacity_hash[shepherd.ava_date] = 0
         shepherd_date_capacity_hash[shepherd.ava_date] += shepherd.capacity
      all_dates = set(sheep_date_freq_hash) | set(shepherd_date_freq_hash)
      sorted_dates = sorted(all_dates)
      #
      all_sheep = 0
      all_shepherd_count = 0
      all_shepherd_capacity = 0
      # print
      print "DATES (sheep count - shepherd count - shepherd capacity - diff):"
      for d in sorted_dates:
         if d in sheep_date_freq_hash:
            all_sheep += sheep_date_freq_hash[d]
         if d in shepherd_date_freq_hash:
            all_shepherd_count += shepherd_date_freq_hash[d]
            all_shepherd_capacity += shepherd_date_capacity_hash[d]
         print d.isoformat()+":", str(all_sheep)+\
                                  "\t"+str(all_shepherd_count)+"\t"+str(all_shepherd_capacity)+\
                                  "\t"+str(all_shepherd_capacity-all_sheep)


def main(args):
    """Prints information about the sheep-shepherd dataset

    Parameters:
    sheep_file -- sheep brief file with normalized values
    shepherd_file -- shepherd brief file with normalized values
    """
    # review parameters
    sheep_file = args[0]
    shepherd_file = args[1]
    # read sheep + shepherds
    normed_sheep_list = Sheep_brief_file.read_sheep_brief_file(sheep_file)
    normed_shepherds_list = Shepherd_brief_file.read_shepherd_brief_file(shepherd_file)
    # perform the matching (including writing output)
    Pasture_info.print_info(normed_sheep_list,normed_shepherds_list)

if __name__ == '__main__':
    args = sys.argv[1:]
    if len(args) == 2:
       main(args)
    else:
       print >> sys.stderr, "Wrong number of parameters."
       print >> sys.stderr, "Usage:"
       print >> sys.stderr, "pasture_info.py sheep_brief_file shepherd_brief_file"
       sys.exit(1)
